package com.beerandjesus.photogallery.thumbnail;

import com.beerandjesus.common.io.ImageFilenameFilter;
import com.beerandjesus.common.io.TokenizedFile;

import java.io.File;
import java.io.IOException;

public class CreateManyThumbnails {

    public static void main(String[] args) {
        if (args.length < 2) {
            System.out.println("Usage: " +
                    "java CreateManyThumbnails <directory> <maximum dimension>");
            return;
        }
        createManyThumbnails(args[0], Integer.parseInt(args[1]));
    }

    public static void createManyThumbnails(String directory, int maxDimension) {
        File path = new File(directory);
        if (!path.exists()) {
            System.out.println("Path does not exist: " + path.getPath());
            return;
        }
        if (!path.isDirectory()) {
            System.out.println("Path is not a directory: " + path.getPath());
            return;
        }
        System.out.println("Processing files from directory " + path.getPath() + "...");
        File[] files = path.listFiles(new ImageFilenameFilter());
        if (files == null) {
            System.out.println("No suitable images found in directory!");
            return;
        }
        for (int i = 0; i < files.length; i++) {
            File file = files[i];
            TokenizedFile tokenizedFile = new TokenizedFile(file);
            String absolutePath = file.getAbsolutePath();
            String thumbnailName = new StringBuffer().append(tokenizedFile.nameRoot()).append(CreateThumbnail.THUMBNAIL_TOKEN_DELIMETER).append(tokenizedFile.extensionWithSeperator()).toString();
            File thumbnail = new File(file.getParentFile(), thumbnailName);
            System.out.println(absolutePath + " --> " + thumbnail.getAbsolutePath());
            try {
                CreateThumbnail.createThumbnail(tokenizedFile, thumbnail, maxDimension);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("Done.");
    }
}
